#include"Library.h"
void deleISBN(LibList* libList, char ISBN[MAX_ISBN])
{
	bookNode* head = libList->head;

	bookNode* dele = NULL;
	//头节点就是要删除的节点
	if (strcmp(head->data.ISBN,ISBN)==0)
	{
		printf("ISBN:%s\t书名:%s\t\t作者:%s\t\t价格:%.2f\t库存容量:%d\n", head->data.ISBN, head->data.name,
			head->data.auth, head->data.price, head->data.inventory);
		dele = head;
		libList->head = libList->head->next;
		delete dele;
		libList->bookNum--;
	}
	else
	{
		while (head)
		{
			if (head->next == NULL)//确保head->next不为空，strcmp不得报错
			{
				return;
			}
			if (strcmp(head->next->data.ISBN, ISBN) == 0)
			{
				printf("ISBN:%s\t书名:%s\t\t作者:%s\t\t价格:%.2f\t库存容量:%d\n", head->next->data.ISBN, head->next->data.name,
					head->next->data.auth, head->next->data.price, head->next->data.inventory);
				dele = head->next;
				if (dele->next == NULL)//表明要删除的是最后一个节点
				{
					head->next = NULL;
					delete dele;
					libList->bookNum--;
				}
				else
				{
					head->next = head->next->next;

					delete dele;
					libList->bookNum--;
				}

			}
			head = head->next;
		}
	}
	
}
void deleName(LibList* libList, char inputName[MAX_BOOK_NAME + 1])
{
	bookNode* head = libList->head;

	bookNode* dele = NULL;
	//头节点就是要删除的节点
	if (strcmp(head->data.name, inputName) == 0)
	{
		printf("ISBN:%s\t书名:%s\t\t作者:%s\t\t价格:%.2f\t库存容量:%d\n", head->data.ISBN, head->data.name,
			head->data.auth, head->data.price, head->data.inventory);
		dele = head;
		libList->head = libList->head->next;
		delete dele;
		libList->bookNum--;
	}
	else
	{
		while (head)
		{
			if (head->next == NULL)//确保head->next不为空，strcmp不得报错
			{
				return;
			}
			if (strcmp(head->next->data.name, inputName) == 0)
			{
				printf("ISBN:%s\t书名:%s\t\t作者:%s\t\t价格:%.2f\t库存容量:%d\n", head->next->data.ISBN, head->next->data.name,
					head->next->data.auth, head->next->data.price, head->next->data.inventory);
				dele = head->next;
				if (dele->next == NULL)//表明要删除的是最后一个节点
				{
					head->next = NULL;
					delete dele;
					libList->bookNum--;
				}
				else
				{
					head->next = head->next->next;

					delete dele;
					libList->bookNum--;
				}

			}
			head = head->next;
		}
	}
}
void deleAuth(LibList* libList, char inputAuth[MAX_AUTH_NAME + 1])
{
	bookNode* head = libList->head;

	bookNode* dele = NULL;
	//头节点就是要删除的节点
	if (strcmp(head->data.auth, inputAuth) == 0)
	{
		printf("ISBN:%s\t书名:%s\t\t作者:%s\t\t价格:%.2f\t库存容量:%d\n", head->data.ISBN, head->data.name,
			head->data.auth, head->data.price, head->data.inventory);
		dele = head;
		libList->head = libList->head->next;
		delete dele;
		libList->bookNum--;
	}
	else
	{
		while (head)
		{
			if (head->next == NULL)//确保head->next不为空，strcmp不得报错
			{
				return;
			}
			if (strcmp(head->next->data.auth, inputAuth) == 0)
			{
				printf("ISBN:%s\t书名:%s\t\t作者:%s\t\t价格:%.2f\t库存容量:%d\n", head->next->data.ISBN, head->next->data.name,
					head->next->data.auth, head->next->data.price, head->next->data.inventory);
				dele = head->next;
				if (dele->next == NULL)//表明要删除的是最后一个节点
				{
					head->next = NULL;
					delete dele;
					libList->bookNum--;
				}
				else
				{
					head->next = head->next->next;

					delete dele;
					libList->bookNum--;
				}

			}
			head = head->next;
		}
	}
}

void functDeleBook(LibList* libList)
{
	bookNode* head = libList->head;
	printf("  删除书籍\n");
	printf("0.ISBN码删除\n");
	printf("1.书名删除\n");
	printf("2.作者删除\n");
	char choice[10];
	printf("请输入你要删除的方式:\n");
	scanf("%s", choice);
	if (choice[0] < '0' || choice[0]>'2' || strlen(choice) != 1)
	{
		printf("你是火星人吗?(任意键程序继续执行)\n");
		return;
	}
	int select = choice[0] - '0';
	switch (select)
	{
	case 0:
		printf("请输入要删除的ISBN码:\n");
		char inputISBN[MAX_ISBN];
		scanf("%s", inputISBN);
		deleISBN(libList, inputISBN);
		break;
	case 1:
		printf("请输入要删除的书名:\n");
		char inputName[MAX_BOOK_NAME + 1];
		scanf("%s", inputName);
		deleName(libList, inputName);
		break;
	case 2:
		printf("请输入要删除的作者名:\n");
		char inputAuth[MAX_AUTH_NAME + 1];
		scanf("%s", inputAuth);
		deleAuth(libList, inputAuth);
		break;
	}
}